/** ============================================================================================== **/
/**						PLANT MANAGER											 				   **/	
/** ============================================================================================== **/

BootApplication.controller("DetPlantController",
			[ "$scope",	"$rootScope", '$translate', "NgTableParams", "DetPlantService", '$http', 'configuration', "MyTankService",
	 function ($scope, 	 $rootScope,   $translate,   NgTableParams,   detPlantService,   $http,   configuration,   myTankService) {
	
	/** INIZIALIZZAZIONI ********************************************************************************************************************************/
	$scope.plantList					= [];
	$scope.filtriAttivi					= {};
	$scope.displayProp					= {};
	$scope.listFilter 					= [];
	$scope.showBoxApplyFilter			= true;
	/****************************************************************************************************************************************************/
	
	/*******************************************************************************************************	GESTIONE FILTER	   ***********	START   */
	$scope.showFilter					= false;
	$scope.hideShowFilter = function () {
		$scope.showFilter = !$scope.showFilter;
	}
	
	$scope.activeFilter = function () {
		console.log("det-plant-ctrl.js		>>>	Applico i filtri");
		$scope.showFilter = false;
		if ($scope.showTable)
			$scope.tableParams.reload();
		else
			$scope.getPlantList();
	}
	
	$scope.removeFilter = function (delFilter) {
		console.log("det-fish-ctrl.js		>>>	Rimuovo i filtri");
		/** Ripulisco la lista dei filtri attivi **/
		$scope.filtriAttivi = [];
		$scope.newListFilter = [];
		$.each( $scope.listFilter, function( index, filter ) {
			if (filter.attr!=delFilter.attr || filter.value!=delFilter.value) { 
				$scope.setFilterObject(filter);
				$scope.newListFilter.push(filter);
			}
		});
		$scope.listFilter=$scope.newListFilter;
		/** Visualizzo/Nascondo i filtri **/
		if ($scope.listFilter.length==0)
			$scope.showFilter = false;
		/** Visualizzo tabella/figure **/
		if ($scope.showTable)
			$scope.tableParams.reload();
		else
			$scope.getFishList();
	}
	
	$scope.setFilterObject = function ( filter ){
		if (filter.attr=="commonName") 		$scope.filtriAttivi.commonName = filter.value;
	}
	/*******************************************************************************************************	GESTIONE FILTER	   ***********	END     */
	
	
	/*******************************************************************************************************	FIGURE VIEW		   ***********	START   */
	$scope.bigCurrentPage 				= 1;
	$scope.itemsPerPage 				= 8;
	$scope.maxSize 						= 5;
	
	$scope.getPlantList = function () {
		console.log("det-plant-ctrl.js		>>>	Recupero la lista delle piante");
		$scope.plantList					= [];
		$scope.displayProp.paginate			= true;
		$scope.displayProp.displayStart		= $scope.bigCurrentPage==1 ? 0 : $scope.itemsPerPage*($scope.bigCurrentPage-1);
		$scope.displayProp.displayLength	= $scope.itemsPerPage;
		detPlantService.getPlantList( $scope.filtriAttivi, $scope.displayProp ).then(function( response ){
			if (response!=null && response.data.success) {
				$.each( response.data.aaData, function( index, plant ) {
					if (plant.urlImage!=null)
						plant.urlImage = configuration.getParam("Constants").ContextPath + plant.urlImage;
					else 
						plant.urlImage = configuration.getParam("Constants").ContextPath + "img/home/plant/box3-1.jpg";
					$scope.plantList.push(plant);	
				});
				$scope.bigTotalItems 	= response.data.iTotalRecords;
				$scope.showFigure		= true;
			} else {
				console.log("Errore nella visualizzazione");
			}
			
			$scope.listFilter = [];
			$.each( $scope.filtriAttivi, function( key, value ) {
				objFilter={};
				objFilter.attr=key;
				objFilter.value=value;
				if (objFilter.value!="") 
					$scope.listFilter.push(objFilter);
			});
		});
	}
	$scope.$watch('idUser', function() {
		if ($rootScope.idUser!=undefined) {
			
			var sPageURL 		= decodeURIComponent(window.location.search.substring(1)), sURLVariables = sPageURL.split('&'), sParameterName, i;
			sParameterName 		= sURLVariables[0].split('=');
			$rootScope.tankId 	= sParameterName[1];
			$scope.tankId		= sParameterName[1];
			
			if ( $rootScope.tankId!=undefined) {
				myTankService.getTank( $rootScope.tankId ).then(function( response ){
					if (response!=null && response.data.success && response.data.aaData!=null) {
						if ( response.data.aaData[0].phMax!=null ) $scope.filtriAttivi.phMax = response.data.aaData[0].phMax;
						if ( response.data.aaData[0].phMin!=null ) $scope.filtriAttivi.phMin = response.data.aaData[0].phMin;
						if ( response.data.aaData[0].ghMax!=null ) $scope.filtriAttivi.ghMax = response.data.aaData[0].ghMax;
						if ( response.data.aaData[0].ghMin!=null ) $scope.filtriAttivi.ghMin = response.data.aaData[0].ghMin;
						if ( response.data.aaData[0].khMax!=null ) $scope.filtriAttivi.khMax = response.data.aaData[0].khMax;
						if ( response.data.aaData[0].khMin!=null ) $scope.filtriAttivi.khMin = response.data.aaData[0].khMin;
						if ( response.data.aaData[0].temperatureMax!=null ) $scope.filtriAttivi.temperatureMax = response.data.aaData[0].temperatureMax;
						if ( response.data.aaData[0].temperatureMin!=null ) $scope.filtriAttivi.temperatureMin = response.data.aaData[0].temperatureMin;
					}
					$scope.getPlantList();
				});
			} else
				$scope.getPlantList();
		}
    });
	
	$scope.setPage = function (pageNo) {
		$scope.bigCurrentPage = pageNo;
		$scope.getPlantList();
	};
	$scope.pageChanged = function() {
		$scope.getPlantList();
	};
	/*******************************************************************************************************	FIGURE VIEW		   ***********	END     */
	
	
	/*******************************************************************************************************	DETTAGLIO PLANT	   ***********	START	*/
	$scope.activeDetailPlant = function(event, plant){
		event.preventDefault();
		$scope.plantModel=plant;
		$scope.showDetailPlant=true;
		$scope.showBoxApplyFilter=false;
	}
	$scope.deactiveDetailPlant = function(event){
		event.preventDefault();
		$scope.plantModel={};
		$scope.showDetailPlant=false;
		$scope.showBoxApplyFilter=true;
	}
	/*******************************************************************************************************	DETTAGLIO PLANT	   ***********	END		*/
	
	$scope.saveLivestock = function( plant ){
		console.log("det-plant-ctrl.js		>>>	Aggiungo la pianta ("+plant.id+") alla vasca ("+$scope.tankId+")");
		detPlantService.saveLivestock( $rootScope.tankId, fish ).then(function( response ){
			if (response!=null && response.data.success && response.data.aaData!=null) {
				console.log("det-plant-ctrl.js		>>>	La pianta ("+plant.id+") e' stata aggiunto alla vasca ("+$scope.tankId+")");
				if ($scope.filtriAttivi.excludePlant==undefined)
					$scope.filtriAttivi.excludePlant = [];
				$scope.filtriAttivi.excludePlant.push(plant.id);
				
				$scope.filtriAttivi.phMax = plant.phMax;
				$scope.filtriAttivi.phMin = plant.phMin;
				$scope.filtriAttivi.ghMax = plant.ghMax;
				$scope.filtriAttivi.ghMin = plant.ghMin;
				$scope.filtriAttivi.khMax = plant.khMax;
				$scope.filtriAttivi.khMin = plant.khMin;
				$scope.filtriAttivi.temperatureMax = plant.temperatureMax;
				$scope.filtriAttivi.temperatureMin = plant.temperatureMin;
				$scope.getPlantList();
			} else {
				console.log("det-plant-ctrl.js		>>>	La pianta ("+plant.id+") NON e' stata aggiunto alla vasca ("+$scope.tankId+")");
			}
		});
	}
}]);